package com.mac.bluetoothbt.feature;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class App extends Application {
    public static final String DEVHISTORY = "devhistory";
    public static final String BLUETOOTHBT = "bluetoothbt";
    private SharedPreferences devHis;
    private DeviceEntity deviceEntity;
    static int timeReadTime = 0;

    public DeviceEntity getDeviceEntity() {
        return deviceEntity;
    }

    public void setDeviceEntity(DeviceEntity deviceEntity) {
        this.deviceEntity = deviceEntity;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        devHis = getSharedPreferences(BLUETOOTHBT, MODE_PRIVATE);
        IntentFilter filter = new IntentFilter();
        filter.addAction(Actions.ACTION_BLE_CONNECTED);
        registerReceiver(new BroadcastReceiver() {
                             @Override
                             public void onReceive(Context context, Intent intent) {
                                if (Actions.ACTION_BLE_CONNECTED.equals(intent.getAction())) {
                                    addDevHis(intent.getStringExtra("mac"));
                                }
                             }
                         }
                , filter);
    }


    public boolean addDevHis(String data) {
        Set<String> devHisSet = getDevHisSet();
        if (devHisSet.contains(data)) {
            return true;
        }
        devHisSet.add(data);
        SharedPreferences.Editor editor = devHis.edit();
        editor.putStringSet(DEVHISTORY, devHisSet);
        boolean result = editor.commit();
        Log.d("degbug", result + "");
        return result;
    }

    public boolean isContained(String data) {
        Set<String> devHisSet = getDevHisSet();
        return devHisSet.contains(data);
    }


    public Set<String> getDevHisSet() {
        return new HashSet<>(devHis.getStringSet(DEVHISTORY, new HashSet<String>()));
    }

}
